import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
    state:{
        user:{
            username:'abc'
        },
        count: 0,
        age: 0
    },
    getters: {
        getterAge(state) {
          return state.age;
        }
    },
    mutations: {
        changeUser(state,data){
            debugger
            state.user = data;
        },
        addCount(state) {
            state.count ++;
        },
        subCount(state, num) {
            //模拟异步请求
            setTimeout(() => {
                state.count -= num;
            }, 2000);
           
        }
    },
    actions:{
        login(context,form){
            axios.post('/api/login',form)
            .then(({data})=>{
                debugger
                console.log(data.data)
                context.commit('changeUser',data.data);
            })
            .catch( (error) => {
                // 处理错误情况
                console.log(error);
            });
        },
    }
})